package com.rahms.server.service;

import com.rahms.common.constant.CacheConstant;
import com.rahms.server.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 缓存服务类
 */
@Service
public class CacheService {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 缓存用户信息
     */
    public void cacheUserInfo(Long userId, Object userInfo) {
        String key = CacheConstant.USER_INFO_CACHE + userId;
        redisUtil.set(key, userInfo, CacheConstant.CACHE_EXPIRE_MEDIUM, TimeUnit.SECONDS);
    }

    /**
     * 获取用户信息缓存
     */
    public Object getUserInfoCache(Long userId) {
        String key = CacheConstant.USER_INFO_CACHE + userId;
        return redisUtil.get(key);
    }

    /**
     * 缓存用户列表
     */
    public void cacheUserList(String role, String keyword, Object userList) {
        String key = CacheConstant.USER_LIST_CACHE + role + ":" + keyword;
        redisUtil.set(key, userList, CacheConstant.CACHE_EXPIRE_MEDIUM, TimeUnit.SECONDS);
    }

    /**
     * 获取用户列表缓存
     */
    public Object getUserListCache(String role, String keyword) {
        String key = CacheConstant.USER_LIST_CACHE + role + ":" + keyword;
        return redisUtil.get(key);
    }

    /**
     * 缓存药品信息
     */
    public void cacheMedicationInfo(Long medicationId, Object medicationInfo) {
        String key = CacheConstant.MEDICATION_INFO_CACHE + medicationId;
        redisUtil.set(key, medicationInfo, CacheConstant.CACHE_EXPIRE_MEDIUM, TimeUnit.SECONDS);
    }

    /**
     * 获取药品信息缓存
     */
    public Object getMedicationInfoCache(Long medicationId) {
        String key = CacheConstant.MEDICATION_INFO_CACHE + medicationId;
        return redisUtil.get(key);
    }

    /**
     * 缓存健康数据统计
     */
    public void cacheHealthDataStatistics(Long elderlyId, String dataType, Object statistics) {
        String key = CacheConstant.HEALTH_DATA_STATISTICS_CACHE + elderlyId + ":" + dataType;
        redisUtil.set(key, statistics, CacheConstant.STATISTICS_CACHE_EXPIRE, TimeUnit.SECONDS);
    }

    /**
     * 获取健康数据统计缓存
     */
    public Object getHealthDataStatisticsCache(Long elderlyId, String dataType) {
        String key = CacheConstant.HEALTH_DATA_STATISTICS_CACHE + elderlyId + ":" + dataType;
        return redisUtil.get(key);
    }

    /**
     * 缓存JWT Token
     */
    public void cacheJwtToken(String token, Object userInfo) {
        String key = CacheConstant.JWT_TOKEN_CACHE_PREFIX + token;
        redisUtil.set(key, userInfo, CacheConstant.JWT_TOKEN_EXPIRE, TimeUnit.SECONDS);
    }

    /**
     * 获取JWT Token缓存
     */
    public Object getJwtTokenCache(String token) {
        String key = CacheConstant.JWT_TOKEN_CACHE_PREFIX + token;
        return redisUtil.get(key);
    }

    /**
     * 删除JWT Token缓存
     */
    public boolean deleteJwtTokenCache(String token) {
        String key = CacheConstant.JWT_TOKEN_CACHE_PREFIX + token;
        return redisUtil.delete(key);
    }

    /**
     * 清除用户相关所有缓存
     */
    public void clearUserCache(Long userId) {
        // 清除用户信息缓存
        redisUtil.delete(CacheConstant.USER_INFO_CACHE + userId);
        // 清除用户列表缓存
        redisUtil.delete(CacheConstant.USER_LIST_CACHE + "*");
    }

    /**
     * 清除药品相关所有缓存
     */
    public void clearMedicationCache() {
        redisUtil.delete(CacheConstant.MEDICATION_CACHE_PREFIX + "*");
    }

    /**
     * 清除健康数据相关所有缓存
     */
    public void clearHealthDataCache() {
        redisUtil.delete(CacheConstant.HEALTH_DATA_CACHE_PREFIX + "*");
    }

    /**
     * 清除所有缓存
     */
    public void clearAllCache() {
        redisUtil.delete("*");
    }
}
